<template>
	<div class="box">
		<div class="title">基础信息</div>
		<div class="container">
			<FormItems
				ref="form"
				v-model="formData"
				:form="formList"
				:state="state"
				labelWidth="100px"></FormItems>
			<orderTable :orderNo="formData.calcSchemeCode" />
		</div>
	</div>
</template>

<script>
	import FormItems from '@/views/finance/manage/formItems/index.vue';
	import FormItem from '@/views/finance/reimburs/formItem/index.vue';
	import orderTable from '../info/orderTable.vue';
	export default {
		inject: ['_dicts', 'details_config', 'deptList', 'details_forecast'],
		components: { FormItems, FormItem, orderTable },
		dicts: [
			'quota_customer_type',
			'quota_apply_type',
			'quota_use_type',
			'quota_vendor_type',
			'quota_lead_source',
			'clues_source',
		],
		props: {
			value: {
				required: false,
			},
			state: {
				required: false,
			},
			type: {
				required: false,
			},
		},
		data() {
			return {};
		},
		computed: {
			details() {
				return this.details_forecast();
			},
			dicts() {
				return this._dicts();
			},
			config() {
				return this.details_config();
			},
			formData: {
				get() {
					return this.useVModel.call(this);
				},
				set(val) {
					this.$emit('input', val);
				},
			},
			formList() {
				return [
					{
						label: '线索来源',
						type: 'select',
						selectList: this.dict.type.clues_source,
						prop: 'leadSourceType',
						rules: true,
					},
					{
						label: '客户类型',
						type: 'select',
						selectList:
							this.config?.quotationCustomerTypeIrrList ?? [],
						prop: 'customerType',
						selectLabel: 'quotationCustomerTypeName',
						selectValue: 'quotationCustomerTypeId',
						rules: true,
						// event: {
						// 	type: 'change',
						// 	cb: row => {
						// 		// row.leaseeId = undefined;
						// 		// row.leaseeName = undefined;
						// 		if (this.formData.productList?.[0]) {
						// 			this.formData.productList[0].maintenanceAnnualMileageKey =
						// 				undefined;
						// 		}
						// 	},
						// },
					},
					{
						label: '申请类型',
						type: 'select',
						selectList: this.dict.type.quota_apply_type,
						prop: 'applyType',
						rules: [
							{
								required: true,
								message: '请选择申请类型',
								trigger: 'change',
							},
						],
						rules: true,
					},
					{
						label: '出租方',
						type: 'select',
						selectList: this.deptList.company,
						selectLabel: 'deptName',
						selectValue: 'deptId',
						prop: 'lessorId',
						event: {
							type: 'change',
							cb: row => {
								this.formData.lessorId =
									this.formData.lessorId?.toString();
								let item = this.deptList.company.find(
									e => e.deptId == row.lessorId,
								);
								this.formData.lessorName = item.deptName;
							},
						},
						rules: true,
					},
					{
						label: '承租方',
						prop: 'leaseeId',
						propLabel: 'leaseeName',
						type: 'select_dialog',
						selectDialogParams: {
							type: 'userNo',
							title: '请选择用户',
							// params: {
							//     customerTypeCode: this.formData.customerType,
							// },
						},
						selectDialogValidate: row => {
							return true;
						},
						cb: (res, row) => {
							this.formData.leaseeId = res.customerId;
							this.formData.leaseeName = res.customerName;
							this.formData.customerTypeCode =
								res.customerTypeCode;
							if (res.customerTypeCode == 1) {
								this.formData.productList.forEach(item => {
									if (item?.plan?.quantityRequired > 1) {
										item.plan.quantityRequired = 1;
									}
								});
							}
							this.$emitter.emit('getTemplateList');
						},
						rules: true,
					},
					{
						label: '租赁用途',
						type: 'select',
						selectList: this.dict.type.quota_use_type,
						prop: 'useType',
						rules: [
							{
								required: true,
								message: '请选择租赁用途',
								trigger: 'change',
							},
						],
					},
					{
						label: '是否厂商业务',
						type: 'radioGroup',
						radioGroupList: this.dict.type.quota_vendor_type,
						prop: 'vendorType',
						rules: true,
					},
					{
						label: '厂商',
						type: 'input',
						prop: 'vendorName',
						show: this.formData.vendorType == '1',
						rules: [
							{
								required: true,
								message: '请输入厂商',
								trigger: 'blur',
							},
						],
					},
					{
						label: '经销商',
						type: 'input',
						prop: 'agency',
						rules: true,
						show: this.config.businessSource == '2',
					},
					{
						label: '项目IRR',
						type: 'text',
						prop: 'irr',
						show: this.type == 'preview',
						format: row => {
							return this.numberRide(row.irr, 100) + '%';
						},
					},
				];
			},
		},
		created() {
			if (this.formData.lessorId == -1) {
				this.formData.lessorId = undefined;
				this.formData.lessorName = undefined;
			}
			if (this.formData.leaseeId == -1) {
				this.formData.leaseeId = undefined;
				this.formData.leaseeName = undefined;
			}
		},
		mounted() {
			if (this.formData?.lessorId) {
				this.formData.lessorId = this.formData.lessorId?.toString();
			}
		},
		methods: {
			valid() {
				return new Promise(resolve => {
					this.$refs.form.form_validate(valid => resolve(valid));
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		margin: 20px;
		padding: 20px;
		border-radius: 8px;
		background-color: #fff;
		.title {
			font-size: 16px;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			color: #333333;
			line-height: 22px;
			padding-bottom: 15px;
			border-bottom: 1px solid #e9e9e9;
		}
	}
</style>
